package com.centaline.androidsalesblog.constants;

import android.content.Context;

import com.centaline.androidsalesblog.model.databaseobject.CityDo;
import com.centaline.androidsalesblog.model.databaseobject.UserDo;
import com.centanet.framework.utils.SprfUtil;

import io.rong.imlib.RongIMClient;

/**
 * 应用参数
 * Created by guilin on 16/1/28.
 */
public final class AppConstant {

    private AppConstant() {
        //Utility Class
    }

    public static boolean IsLogin;
    public static String UserId;
    public static String CityCode;
    public static String CityName;
    public static String SaleUrl;
    public static String NewEstUrl;
    public static String UserCenterUrl;
    public static String BizCommonUrl;
    public static String AdLink;
    public static String AgentPhoto;
    public static boolean NearbyEnable;
    public static boolean ShowTalk;
    public static RongIMClient.ConnectionStatusListener.ConnectionStatus connectionStatus;
    public static UserDo userDo;
    public static String IP;

    /**
     * 初始化
     */
    public static void init(Context context) {
        IsLogin = SprfUtil.getBoolean(context, SprfConstant.IS_LOGIN, false);
        UserId = SprfUtil.getString(context, SprfConstant.USER_ID, "");
        CityCode = SprfUtil.getString(context, SprfConstant.CITY_CODE, "");
        CityName = SprfUtil.getString(context, SprfConstant.CITY_NAME, "");
        SaleUrl = SprfUtil.getString(context, SprfConstant.SALE_URL, "");
        NewEstUrl = SprfUtil.getString(context, SprfConstant.NEW_EST_URL, "");
        UserCenterUrl = SprfUtil.getString(context, SprfConstant.USER_CENTER_URL, "");
        BizCommonUrl = SprfUtil.getString(context, SprfConstant.BIZ_COMMON_URL, "");
        AdLink = SprfUtil.getString(context, SprfConstant.AD_LINK, "");
        AgentPhoto = SprfUtil.getString(context, SprfConstant.AGENT_PHOTO, "");
        NearbyEnable = SprfUtil.getBoolean(context, SprfConstant.NEARBY_ENABLE, false);
    }

    /**
     * 切换市
     */
    public static void exchangeCity(Context context, CityDo cityDo) {
        CityCode = cityDo.getCode();
        CityName = cityDo.getCityName();
        SaleUrl = cityDo.getAndroid();
        NewEstUrl = cityDo.getNewPropUrl();
        UserCenterUrl = cityDo.getUserCenterUrl();
        BizCommonUrl = cityDo.getBizcommonUrl();
        AdLink = cityDo.getAdLink();
        AgentPhoto = cityDo.getAgentPhoto();
        SprfUtil.setString(context, SprfConstant.CITY_CODE, CityCode);
        SprfUtil.setString(context, SprfConstant.CITY_NAME, CityName);
        SprfUtil.setString(context, SprfConstant.SALE_URL, SaleUrl);
        SprfUtil.setString(context, SprfConstant.NEW_EST_URL, NewEstUrl);
        SprfUtil.setString(context, SprfConstant.USER_CENTER_URL, UserCenterUrl);
        SprfUtil.setString(context, SprfConstant.BIZ_COMMON_URL, BizCommonUrl);
        SprfUtil.setString(context, SprfConstant.AD_LINK, AdLink);
        SprfUtil.setString(context, SprfConstant.AGENT_PHOTO, AgentPhoto);
    }

    /**
     * 附近是否可用
     */
    public static void setNearbyEnable(Context context, boolean enable) {
        NearbyEnable = enable;
        SprfUtil.setBoolean(context, SprfConstant.NEARBY_ENABLE, enable);
    }

    /**
     * 设置用户登录状态
     */
    public static void setUserLogin(Context context, UserDo newUser) {
        if (newUser == null) {//退出
            IsLogin = false;
            userDo = null;
            UserId = null;
            SprfUtil.setBoolean(context, SprfConstant.IS_LOGIN, false);
            SprfUtil.setString(context, SprfConstant.USER_ID, "");
        } else {
            IsLogin = true;
            userDo = newUser;
            UserId = newUser.getUserId();
            SprfUtil.setBoolean(context, SprfConstant.IS_LOGIN, true);
            SprfUtil.setString(context, SprfConstant.USER_ID, newUser.getUserId());
        }
    }
}
